import java.util.HashMap;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> charMap = new HashMap<>();
        int lp = 0, rp = 0, max_len = 0;
        while (rp < s.length()) {
            char c = s.charAt(rp);
            rp++;
            if (!charMap.containsKey(c) || charMap.get(c) == 0) {
                charMap.put(c, 1);
                max_len = Math.max(max_len, rp - lp);
            } else {
                while (lp < rp) {
                    char l = s.charAt(lp);
                    lp++;
                    if (l == c) break;
                    int nCnt = charMap.get(l) - 1;
                    charMap.put(l, nCnt);
                }
            }
        }
        return max_len;
    }
}

public class Main {
    static Solution solution = new Solution();

    public static void main(String[] args) {
        String str = "abcabcbb";
        System.out.println(solution.lengthOfLongestSubstring(str));
    }
}